{
语法百题 解题报告：
 http://git.oschina.net/et0662/sznoi-grammar/ 
}
var
	t:char;
	x,n,i:integer;
	caps:boolean;
begin
	readln(n);
	for i:=1 to n do
	begin
		x:=0;
		caps:=false;
		while not eoln do
		begin
			read(t);
			if ( ord('a')<=ord(t)) and (ord(t)<=ord('z')) then
			begin
				if not caps then
					inc(x)
				else
				begin
					x:=x+2;
					caps:=false;
				end;
			end;
			if ( ord('A')<=ord(t)) and (ord(t)<=ord('Z')) then
			begin
				if caps then
					inc(x)
				else
				begin
					x:=x+2;
					caps:=true;
				end;
			end;
		end;
		readln;
		writeln(x);
	end;
end.
